#####################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#####################################################
# Figure 4: Share of supporters in control vs treatment
#####################################################

# define which packages are needed 
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

# load data
load("EUP_Costs_ReplicationData.RData")

# Experiment 1: Baseline Support in Control Group by Country
supportExp1_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement > 0.5, 1,0)) %>%
  dplyr::group_by(QCOUNTRY) %>%
  mutate(meanSupport = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupport) %>%
  unique()

# Support: Experiment 1, Treatment SoldiersRisk by Country
supportExp1_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "SoldiersRisk" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupportTreat) %>%
  unique()

# Support: Experiment 1, Treatment CountryCost by Country
supportExp1_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "CountryCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T))  %>%
  dplyr::select(QCOUNTRY, iso, meanSupportTreat) %>%
  unique()

# Support: Treatment Experiment 1, SharedCost by Country
supportExp1_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "SharedCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupportTreat) %>%
  unique()

# Experiment 2: Baseline Support in Control Group by Country
supportExp2_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupport = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupport) %>%
  unique()

# Support in Treatment Group: Experiment 2, Treatment JobCuts by Country
supportExp2_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "JobCuts" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupportTreat) %>%
  unique()

# Support in Treatment Group: Experiment 2, Treatment CountryCost by Country
supportExp2_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "CountryCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanSupportTreat) %>%
  unique()

# Support in Treatment Group: Experiment 2, Treatment SharedCost by Country
supportExp2_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "SharedCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft > 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanSupportTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso,  meanSupportTreat) %>%
  unique()

# Information on Experiment
supportExp1_baselineCOUNTRY$experiment <- "Support for Military Intervention"
supportExp2_baselineCOUNTRY$experiment <- "Support for Defence Procurement"


## Country Comparisons: Support in Control Group vs. Support in Treatment Group
# Support: Experiment 1
supportExp1_Treat1_comp <- left_join(supportExp1_baselineCOUNTRY, 
                                     supportExp1_Treat1_COUNTRY,
                                     by = join_by(QCOUNTRY, iso)) # Control vs. Casualties
supportExp1_Treat21_comp <- left_join(supportExp1_baselineCOUNTRY, 
                                      supportExp1_Treat21_COUNTRY,
                                      by = join_by(QCOUNTRY, iso)) # Control vs. National Budget
supportExp1_Treat22_comp <- left_join(supportExp1_baselineCOUNTRY, 
                                      supportExp1_Treat22_COUNTRY,
                                      by = join_by(QCOUNTRY, iso)) # Control vs. Pooled Budget

# Support: Experiment 2
supportExp2_Treat1_comp <- left_join(supportExp2_baselineCOUNTRY, 
                                     supportExp2_Treat1_COUNTRY,
                                     by = join_by(QCOUNTRY, iso)) # Control vs. Job Cuts
supportExp2_Treat21_comp <- left_join(supportExp2_baselineCOUNTRY, 
                                      supportExp2_Treat21_COUNTRY,
                                      by = join_by(QCOUNTRY, iso)) # Control vs. National Budget
supportExp2_Treat22_comp <- left_join(supportExp2_baselineCOUNTRY, 
                                      supportExp2_Treat22_COUNTRY,
                                      by = join_by(QCOUNTRY, iso)) # Control vs. Pooled Budget



### Support Plots

## Support: Experiment 1, Treatment 1 (Casualties)
# Red Colour for countries with changing majorities
supportExp1_Treat1_comp$red <- ifelse(supportExp1_Treat1_comp$meanSupport > 0.5 & supportExp1_Treat1_comp$meanSupportTreat < 0.5 |
                                        supportExp1_Treat1_comp$meanSupport < 0.5 & supportExp1_Treat1_comp$meanSupportTreat > 0.5,
                                      TRUE, FALSE)

# Plot Results
plotDescrEx1_Treat1 <- supportExp1_Treat1_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Military Intervention: Casualties") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 0.5) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


## Support: Experiment 1, Treatment 2.1 (National Budget)
# Red Colour for countries with changing majorities
supportExp1_Treat21_comp$red <- ifelse(supportExp1_Treat21_comp$meanSupport > 0.5 & supportExp1_Treat21_comp$meanSupportTreat < 0.5 |
                                         supportExp1_Treat21_comp$meanSupport < 0.5 & supportExp1_Treat21_comp$meanSupportTreat > 0.5,
                                       TRUE, FALSE)

# Plot Results
plotDescrEx1_Treat21 <- supportExp1_Treat21_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Military Intervention: National Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


## Support: Experiment 1, Treatment 2_2 (Pooled Budgets)
# Red Colour for countries with changing majorities
supportExp1_Treat22_comp$red <- ifelse(supportExp1_Treat22_comp$meanSupport > 0.5 & supportExp1_Treat22_comp$meanSupportTreat < 0.5 |
                                         supportExp1_Treat22_comp$meanSupport < 0.5 & supportExp1_Treat22_comp$meanSupportTreat > 0.5,
                                       TRUE, FALSE)

# Plot Results
plotDescrEx1_Treat22 <- supportExp1_Treat22_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Military Intervention: Pooled Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


## Support: Experiment 2, Treatment 1 (Job Cuts)
# Red Colour for countries with changing majorities
supportExp2_Treat1_comp$red <- ifelse(supportExp2_Treat1_comp$meanSupport > 0.5 & supportExp2_Treat1_comp$meanSupportTreat < 0.5 |
                                        supportExp2_Treat1_comp$meanSupport < 0.5 & supportExp2_Treat1_comp$meanSupportTreat > 0.5,
                                      TRUE, FALSE)

# Plot Results
plotDescrEx2_Treat1 <- supportExp2_Treat1_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Defence Procurement: Job Cuts") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

## Support: Experiment 2, Treatment 2_1 (National Budget)
# Red Colour for countries with changing majorities
supportExp2_Treat21_comp$red <- ifelse(supportExp2_Treat21_comp$meanSupport > 0.5 & supportExp2_Treat21_comp$meanSupportTreat < 0.5 |
                                         supportExp2_Treat21_comp$meanSupport < 0.5 & supportExp2_Treat21_comp$meanSupportTreat > 0.5,
                                       TRUE, FALSE)

# Plot Results
plotDescrEx2_Treat21 <- supportExp2_Treat21_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Defence Procurement: National Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

## Support: Experiment 2, Treatment 2_2 (Pooled Budget)
# Red Colour for countries with changing majorities
supportExp2_Treat22_comp$red <- ifelse(supportExp2_Treat22_comp$meanSupport > 0.5 & supportExp2_Treat22_comp$meanSupportTreat < 0.5 |
                                         supportExp2_Treat22_comp$meanSupport < 0.5 & supportExp2_Treat22_comp$meanSupportTreat > 0.5,
                                       TRUE, FALSE)

# Plot Results
plotDescrEx2_Treat22 <- supportExp2_Treat22_comp %>%
  ggplot(., aes(x = meanSupportTreat, y = meanSupport, label = iso, country = iso, color = red)) +
  labs(x = "Support in Treatment Group", y = "Support in Control Group",
       title = "Defence Procurement: Pooled Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

## arrange plots
all_plots_sup <- grid.arrange(plotDescrEx1_Treat1, plotDescrEx1_Treat21, plotDescrEx1_Treat22,
                              plotDescrEx2_Treat1, plotDescrEx2_Treat21, plotDescrEx2_Treat22, nrow = 2)

# save plots 
ggsave("plots/SupportControlTreatmentComparison.png", all_plots, device = "jpeg", width = 4800 , height = 3200, units = "px")
